﻿using Language.Resource;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Fms;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Fms;
using VisionCloud.Service.Ms;

namespace VisionCloud.Api.Controllers.Fms
{
    /// <summary>
    /// Fms项目相关
    /// </summary>
    [SystemModule("Fms项目相关")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/fms/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "Fms项目相关")]
    public class ProjectController : ApiBaseController
    {

        public HouseMapService HouseMapService { get; set; }

        public ProjectBuildingFloorService BuildingFloorService { get; set; }

        /// <summary>
        /// 公共多语言资源
        /// </summary>
        public IStringLocalizer<CommonResource> CommonLocalizer { get; set; }

        /// <summary>
        /// 分页获取FMS项目列表（附带楼栋楼层）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "管理" })]
        [SwaggerResponse(0, "PageData字段为项目列表", typeof(FmsProjectBuildingFloorListDto))]
        public async Task<ApiResult> GetList([FromBody] ProjectBuildlingFloorSearchDto dto)
        {
            return await TryCatchAsync(BuildingFloorService.GetList(dto));
        }

        /// <summary>
        /// 根据户型图Id查询户型图绑定FMS楼栋楼层信息
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>Result不为null则表示已绑定FMS楼栋楼层</remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "管理" })]
        [SwaggerResponse(0, "Result字段为绑定信息", typeof(HouseMapBuildingFloorResponseDto))]
        public async Task<ApiResult> GetHouseMapInfo([FromBody] HouseMapBindDto dto)
        {
            return await HouseMapService.GetHouseMapBuildingFloor(dto.HouseMapId);
        }

        /// <summary>
        /// 更新户型图FMS项目楼栋楼层信息
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// Code = 0，表示绑定成功  
        /// Code != 1，表示绑定失败
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "管理" })]
        public async Task<ApiResult> UpdateHouseMapInfo([FromBody] UpdateHouseMapBuildingFloorDto dto)
        {
            return await HouseMapService.UpdateHouseMapBuildingFloor(dto);
        }

        /// <summary>
        /// 通过FMS项目的BimMapCode得到项目模型文件，并调用自动标注算法返回户型图地图和站点位置信息
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// Code = 0，表示绑定成功  
        /// Code != 0，表示失败
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "管理" })]
        [SwaggerResponse(0, "Result字段为返回的户型图Id", typeof(ApiResult))]
        public async Task<ApiResult> AddHouseMap([FromBody] AddHouseMapDto dto)
        {
            await Contracts.Frequency($"housemap_add_limit_{dto.ProjectId}", 1, TimeSpan.FromSeconds(3), CommonLocalizer["YB0449"].Value);
            await Contracts.Frequency($"housemap_add_limit_all_{Guid.NewGuid()}", 10, TimeSpan.FromSeconds(90), CommonLocalizer["YB0449"].Value, false);
            return await HouseMapService.CreateHouseMapByArithmetic(dto);
        }
    }
}
